﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.Data;
using CRMMis.BLL;

public partial class CRM_Manager_ERP_CargoEdit : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState_ObjID = string.IsNullOrEmpty(Request.QueryString["ObjId"]) ? "Add" : Request.QueryString["ObjId"];
            ViewState_DetailId = "0";
            StoreBind();
            InitData();
        }
    }

    protected void StoreBind()
    {
        DataTable dtType, dtUse, dtSupply = null;
        dtType = ERP_Type.GetList("1=1", "Type_ID ASC").Tables[0];
        dtUse = ERP_Use.GetList("1=1", "Use_ID ASC").Tables[0];
        dtSupply = ERP_Supply.GetList("DelState=0", "SupplyID ASC").Tables[0];
        Store_Type.DataSource = dtType;
        Store_Type.DataBind();
        Store_Use.DataSource = dtUse;
        Store_Use.DataBind();
        Store_Supply.DataSource = dtSupply;
        Store_Supply.DataBind();
        dtType.Dispose();
        dtUse.Dispose();
        dtSupply.Dispose();
    }


    [DirectMethod]
    public void SetCombCurrentnameSelect(string CargoID)
    {
        ViewState_ObjID = CargoID;
        DataTable dt = CRMMis.BLL.ERP_Cargo.GetList(" CargoID=" + ViewState_ObjID).Tables[0];
        SetContainer(CargoPanel, dt);
        //ViewState_ObjID = extcomb_CargoName.SelectedItem.Value;
        //DataTable dt = ERP_Cargo.GetList("DelState<>1 and CargoID=" + CargoID).Tables[0];
        //InitData();
    }

    private void InitData()
    {
        if (ViewState_ObjID != "Add")
        {
            DataTable dt = CRMMis.BLL.ERP_Cargo.GetList(" CargoID=" + ViewState_ObjID).Tables[0];
            ViewState_DetailId = dt.Rows[0]["DetailId"].ToString();
            SetContainer(CargoPanel, dt);
            if (dt.Rows[0]["DelState"].Equals(-1))
            {
                extBtn_Save.Disabled = false;
                extBtn_Save.Hidden = false;
                extBtn_Submit.Disabled = false;
                extBtn_Submit.Hidden = false;
                extBtn_CheckInfo.Disabled = true;
                extBtn_CheckInfo.Hidden = true;
            }
            else
            {
                if (dt.Rows[0]["State"].Equals(0))
                {
                    extBtn_Save.Disabled = true;
                    extBtn_Save.Hidden = true;
                    extBtn_Submit.Disabled = false;
                    extBtn_Submit.Hidden = false;
                    extBtn_CheckInfo.Disabled = false;
                    extBtn_CheckInfo.Hidden = false;
                }
                if (dt.Rows[0]["State"].Equals(1))
                {
                    if (dt.Rows[0]["Detail_State"].Equals(0))
                    {
                        extBtn_Save.Disabled = true;
                        extBtn_Save.Hidden = true;
                        extBtn_Submit.Disabled = false;
                        extBtn_Submit.Hidden = false;
                        extBtn_CheckInfo.Disabled = false;
                        extBtn_CheckInfo.Hidden = false;
                    }
                    if (dt.Rows[0]["Detail_State"].Equals(1))
                    {
                        extBtn_Save.Disabled = true;
                        extBtn_Save.Hidden = true;
                        extBtn_Submit.Disabled = true;
                        extBtn_Submit.Hidden = true;
                        extBtn_CheckInfo.Disabled = true;
                        extBtn_CheckInfo.Hidden = true;
                    }
                }
            }
        }
        else
        {
            extBtn_Save.Disabled = false;
            extBtn_Save.Hidden = false;
            extBtn_Submit.Disabled = true;
            extBtn_Submit.Hidden = true;
            extBtn_CheckInfo.Disabled = true;
            extBtn_CheckInfo.Hidden = true;
        }
    }

    protected void MyData_Refresh(object sender, StoreRefreshDataEventArgs e)
    {
        BindGridData(e);
    }

    private void BindGridData(StoreRefreshDataEventArgs e)
    {
        int recordcount = ERP_Cargo.GetRecorderCount(GetMatQryString());
        int PageSize = extcomb_CargoName.PageSize;
        int CurPage = e.Start / PageSize + 1;
        e.Total = recordcount;
        DataTable tmpDT = ERP_Cargo.GetPageList(PageSize, CurPage, GetMatQryString(), "CargoName,Spec,Area,Version,ProduceName").Tables[0];
        store_CargoName.DataSource = tmpDT;
        store_CargoName.DataBind();
    }

    private string GetMatQryString()
    {
        string StrSql = " 1=1 and DelState<>1";
        if (!string.IsNullOrEmpty(extcomb_CargoName.SelectedItem.Text.Trim()))
            StrSql += "  and  ( CargoName like '%" + FilterSpecChar(extcomb_CargoName.SelectedItem.Text) + "%'  or Spell like '%" + FilterSpecChar(extcomb_CargoName.SelectedItem.Text) + "%' or ByName like '%" + FilterSpecChar(extcomb_CargoName.SelectedItem.Text) + "%' or CargoCode like '%" + FilterSpecChar(extcomb_CargoName.SelectedItem.Text) + "%')";
        return StrSql;
    }

    [DirectMethod]
    public void SaveData(string type)
    {
        try
        {
            if (type == "submit")
            {
                string strWhere = " CargoName = '" + FilterSpecChar(extcomb_CargoName.SelectedItem.Text.Trim()) + "' and Spec='" + FilterSpecChar(exttxtf_Spec.Text.Trim()) + "' and Area='" + FilterSpecChar(exttxtf_Area.Text.Trim()) + "' and Version='" + FilterSpecChar(exttxtf_Version.Text.Trim()) + "' and ProduceName='" + FilterSpecChar(exttxtf_ProduceName.Text.Trim()) + "'";
                DataTable dts = ERP_Cargo.GetList(strWhere).Tables[0];
                if (dts.Rows.Count == 0)
                {
                    throw new Exception("物品不存在,请先保存！");
                }
                else
                {
                    if (ViewState_ObjID.ToString() != dts.Rows[0]["CargoId"].ToString())
                    {
                        throw new Exception("物品信息被更换，请先进行保存！");
                    }
                }
            }
            int old_DetailId = 0;
            CRMMis.Model.ERP_Cargo oModel = new CRMMis.Model.ERP_Cargo();
            CRMMis.Model.ERP_Cargo_Detail dModel = new CRMMis.Model.ERP_Cargo_Detail();
            string sWhere = " CargoName = '" + FilterSpecChar(extcomb_CargoName.SelectedItem.Text.Trim()) + "' and Spec='" + FilterSpecChar(exttxtf_Spec.Text.Trim()) + "' and Area='" + FilterSpecChar(exttxtf_Area.Text.Trim()) + "' and Version='" + FilterSpecChar(exttxtf_Version.Text.Trim()) + "' and ProduceName='" + FilterSpecChar(exttxtf_ProduceName.Text.Trim()) + "'";
            DataTable dtM = ERP_Cargo.GetList(sWhere).Tables[0];
            if (dtM.Rows.Count > 0)
            {
                ViewState_ObjID = dtM.Rows[0]["CargoId"].ToString();
                if (ViewState_ObjID != "add")
                {
                    oModel = ERP_Cargo.GetModel(int.Parse(ViewState_ObjID));
                    sWhere = "CargoId <>" + oModel.CargoId + " and  CargoName = '" + FilterSpecChar(extcomb_CargoName.SelectedItem.Text.Trim()) + "' and Spec='" + FilterSpecChar(exttxtf_Spec.Text.Trim()) + "' and Area='" + FilterSpecChar(exttxtf_Area.Text.Trim()) + "' and Version='" + FilterSpecChar(exttxtf_Version.Text.Trim()) + "' and ProduceName='" + (exttxtf_ProduceName.Text.Trim()) + "'";
                    if (ERP_Cargo.GetRecorderCount(sWhere) > 0)
                        throw new Exception("物品已存在！");
                    DataTable dt = ERP_Cargo_Detail.GetList("CargoId='" + oModel.CargoId + "' and State=1", "DetailId DESC").Tables[0];
                    if (dt.Rows.Count > 0)
                        old_DetailId = int.Parse(dt.Rows[0]["DetailId"].ToString());
                    if (type == "submit" || type == "check")
                    {
                        dModel = ERP_Cargo_Detail.GetModel(int.Parse(ViewState_DetailId));
                    }
                    else
                    {
                        DataTable dtb = ERP_Cargo_Detail.GetList("CargoId='" + oModel.CargoId + "' and State=0", "DetailId DESC").Tables[0];
                        if (dtb.Rows.Count > 0)
                        {
                            if (dtb.Rows[0]["DetailId"].ToString() == ViewState_DetailId)
                            {
                                if (ViewState_DetailId != "0")
                                {
                                    dModel = ERP_Cargo_Detail.GetModel(int.Parse(ViewState_DetailId));
                                    type = "update";
                                }
                                if (ViewState_DetailId == "0")
                                {
                                    dModel.DetailId = ERP_Cargo_Detail.GetNewID();
                                    type = "addD";
                                }
                            }
                            else
                            {
                                dModel.DetailId = ERP_Cargo_Detail.GetNewID();
                                type = "addD";
                            }
                        }
                        else
                        {
                            dModel.DetailId = ERP_Cargo_Detail.GetNewID();
                            type = "addD";
                        }
                        
                    }
                }
            }
            else
            {
                ViewState_ObjID = "add";
                dModel.DetailId = ERP_Cargo_Detail.GetNewID();
            }
            oModel.CargoName = extcomb_CargoName.SelectedItem.Text.Trim().ToString();
            oModel.Spell = exttxtf_Spell.Text.Trim();
            oModel.ByName = exttxtf_ByName.Text.Trim();
            oModel.CargoCode = exttxtf_CargoCode.Text.Trim();
            oModel.Spec = exttxtf_Spec.Text.Trim();
            if (extcomb_TypeId.SelectedItem.Text != "")
                oModel.TypeId = int.Parse(extcomb_TypeId.SelectedItem.Value);
            if (extcomb_UseId.SelectedItem.Text != "")
                oModel.UseId = int.Parse(extcomb_UseId.SelectedItem.Value);
            oModel.Area = exttxtf_Area.Text.Trim();
            oModel.Version = exttxtf_Version.Text.Trim().ToString();
            oModel.ProduceName = exttxtf_ProduceName.Text.Trim().ToString();
            oModel.Unit = exttxtf_Unit.Text.Trim();
            oModel.IsImported = extradio_ii1.Checked ? 0 : 1;
            oModel.Pic = "";
            oModel.InUser = BS_LoginUserID;
            oModel.InDate = System.DateTime.Now.ToString();

            dModel.Supplyprice = Math.Round(decimal.Parse(exttxtf_Supplyprice.Text.Trim()), 4);
            dModel.SupplyId = int.Parse(extcomb_SupplyId.SelectedItem.Value);
            dModel.Remark = exttxtf_Remark.Text.Trim();
            if (type == "add")
            {
                oModel.State = 0;
                oModel.DelState = -1;
                dModel.State = 0;
                dModel.Inuser = BS_LoginUserID;
                dModel.InDate = oModel.InDate;
            }
            if (type == "addD")
            {
                dModel.State = 0;
                dModel.Inuser = BS_LoginUserID;
                dModel.InDate = oModel.InDate;
            }
            if (type == "update")
            {
                dModel.State = 0;
                dModel.Inuser = BS_LoginUserID;
                dModel.InDate = oModel.InDate;
            }
            if (type == "submit")
            {
                oModel.State = 0;
                oModel.DelState = 0;
                dModel.State = 0;
            }
            if (type == "check")
            {
                oModel.State = 1;
                oModel.DelState = 0;
                dModel.State = 1;
            }
            string err = ERP_Cargo.Update(oModel, dModel, old_DetailId, type);
            if (err != "")
                throw new Exception(err);
            string handler = "";
            if (err == "")
                handler = "closeCurrTab();";
            X.Msg.Alert("系统提示", "保存成功！", handler).Show();
        }
        catch (Exception ex)
        {
            var err = ex.Message;
            X.Msg.Alert("系统提示", err).Show();
        }
        finally
        {
            GC.Collect();
        }
    }
}